package com.ruoyi.web.controller.salary.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Salary;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISalaryService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-06-17
 */
@Controller
@RequestMapping("/system/salary")
public class SalaryController extends BaseController
{
    private String prefix = "system/salary";

    @Autowired
    private ISalaryService salaryService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("system:salary:view")
    @GetMapping()
    public String salary()
    {
        return prefix + "/salary";
    }

    @RequiresPermissions("system:salary:view")
    @GetMapping("/money/{userId}")
    public String money(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("userId", userId);
        return prefix + "/money";
    }

    /**
     * 查询未下发工资
     */
    @RequiresPermissions("system:salary:listsBalance")
    @PostMapping("/listsBalances")
    @ResponseBody
    public  TableDataInfo listsBalance(Long sName,Salary salar)
    {
        startPage();
        salar.setsName(sName);
        List<Salary> list = salaryService.selectSalaryListsBalances(salar);
        Salary salary1=new Salary();
//        SysUser sysUser=new SysUser();
        for(Salary salary:list){
            if(salary.getsMoney().equals(salary.getsBalance())){
                salary1.setsId(salary.getsId());
                salary1.setsStatus(0);
                salaryService.updateSalary(salary1);
            }
        }

        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:salary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Salary salary)
    {
        startPage();
        List<Salary> list = salaryService.selectSalaryList(salary);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:salary:list")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long userId, Date sdate, Date edate)
    {
        startPage();
        List<Salary> list = salaryService.lists(userId,sdate,edate);
        return getDataTable(list);
    }

    /**
     * 查询对应人员姓名
     */
    @RequiresPermissions("system:salary:listuser")
    @PostMapping("/listname")
    @ResponseBody
    public SysUser listuser(Long userId)
    {
       return sysUserService.selectUserById(userId);
    }

    /**
     * 查询未下发工资
     */
    @RequiresPermissions("system:salary:listsBalance")
    @PostMapping("/listsBalance")
    @ResponseBody
    public  TableDataInfo listsBalance(Long sName)
    {
        startPage();
        List<Salary> list = salaryService.selectSalaryListsBalance(sName);
        Salary salary1=new Salary();
//        SysUser sysUser=new SysUser();
        for(Salary salary:list){
            if(salary.getsMoney().compareTo(salary.getsBalance())==0){
                salary1.setsId(salary.getsId());
                salary1.setsStatus(0);
                salaryService.updateSalary(salary1);
            }
        }
        return getDataTable(list);
    }



//    /**
//     * 导出【请填写功能名称】列表
//     */
//    @RequiresPermissions("system:salary:export")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(Salary salary,Long sName,Long sId)
//    {
////        salary.setsName(sName);
//        salary.setsId(sId);
//        List<Salary> list = salaryService.selectSalaryList(salary);
//        ExcelUtil<Salary> util = new ExcelUtil<Salary>(Salary.class);
//        return util.exportExcel(list, "salary");
//    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SysUser sysuser = sysUserService.selectUser(userId);
        mmap.put("sysuser", sysuser);
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:salary:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Salary salary)
    {
        BigDecimal bigDecimal=BigDecimal.valueOf(0.0D);
        salary.setsBalance(bigDecimal);
        SysUser sysuser = sysUserService.selectUser(salary.getsName());
        SysUser sysUser=new SysUser();
        sysUser.setUserId(salary.getsName());
        if(salary.getsMoney().compareTo(BigDecimal.ZERO)==0||salary.getsDate()==null){
            return toAjax(0);
        }
        BigDecimal bigDecimal1=BigDecimal.valueOf(0.0D);
        if(sysuser.getBalance()!=null&&sysuser.getBalance()!=bigDecimal1){

            sysUser.setBalance(salary.getsMoney().add(sysuser.getBalance()));
            sysUserService.updateUser(sysUser);
        }else{
            sysUser.setBalance(salary.getsMoney());
            sysUserService.updateUser(sysUser);
        }
        return toAjax(salaryService.insertSalary(salary));
    }

    /**
     * 下发
     */
    @GetMapping("/detail/{sId}")
    public String detail(@PathVariable("sId") Long sId, ModelMap mmap)
    {
        Salary salary = salaryService.selectSalaryById(sId);
        mmap.put("salary", salary);
        return prefix + "/detail";
    }

    /**
     * 下发保存
     */
    @RequiresPermissions("system:salary:detail")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/detail")
    @ResponseBody
    public AjaxResult detailSave(Salary salary)
    {
        Salary salary1=new Salary();
        SysUser sysUser=new SysUser();
        SysUser sysUser1=  sysUserService.selectUserById(salary.getsName());
        Salary salary2 =salaryService.selectSalaryById(salary.getsId());
       //判断个人原先工资是否大于下发工资 下发金额等于null
        if(salary2.getsMoney().compareTo(salary.getsMoney())==1&&salary2.getsBalance().compareTo(BigDecimal.ZERO)==0){
            salary1.setsId(salary.getsId());
            salary1.setsBalance(salary.getsMoney());
            salary1.setsStatus(2);
            sysUser.setUserId(salary.getsName());
            sysUser.setBalance(sysUser1.getBalance().subtract(salary.getsMoney()));
            sysUserService.updateUser(sysUser);
            return toAjax(salaryService.updateSalary(salary1));
        //判断个人原先工资是否大于下发工资下发金额不等于null
        }else if(salary2.getsMoney().compareTo(salary.getsMoney())==1&&salary.getsMoney()!=null&&salary2.getsBalance()!=BigDecimal.ZERO){
            salary1.setsId(salary.getsId());
            salary1.setsBalance(salary.getsMoney().add(salary2.getsBalance()));
            if((salary.getsMoney().add(salary2.getsBalance()))==salary2.getsMoney()){
                salary1.setsStatus(0);
            }else if((salary.getsMoney().add(salary2.getsBalance())).compareTo(salary2.getsMoney())==1){
                return toAjax(0);
            }
            sysUser.setUserId(salary.getsName());
            sysUser.setBalance(sysUser1.getBalance().subtract(salary.getsMoney()));
            sysUserService.updateUser(sysUser);
            return toAjax(salaryService.updateSalary(salary1));
            //判断个人原先工资是否小于下发工资 如果小于返回0 回滚
        }else if (salary2.getsMoney().compareTo(salary.getsMoney())==-1){
                return toAjax(0);
            //判断个人原先工资是减已下发金额是否大于输入下发金额 如果大于返回0 回滚
        }else if (salary2.getsBalance().compareTo(BigDecimal.ZERO)==1&&(salary2.getsMoney().subtract(salary2.getsBalance())).compareTo(salary.getsMoney())==-1){
            return toAjax(0);
        }else{
            salary1.setsId(salary.getsId());
            salary1.setsBalance(salary.getsMoney());
            salary1.setsStatus(0);
            sysUser.setUserId(salary.getsName());
            sysUser.setBalance(sysUser1.getBalance().subtract(salary.getsMoney()));
            sysUserService.updateUser(sysUser);
            return toAjax(salaryService.updateSalary(salary1));
        }
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{sId}")
    public String edit(@PathVariable("sId") Long sId, ModelMap mmap)
    {
        Salary salary = salaryService.selectSalaryById(sId);
        mmap.put("salary", salary);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】同时修改个人余额
     */
    @RequiresPermissions("system:salary:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Salary salary)
    {
        Salary salary1 =salaryService.selectSalaryById(salary.getsId());
        SysUser sysUser1=  sysUserService.selectUserById(salary.getsName());
        SysUser sysUser=new SysUser();
            //判断修改前与修改后的工资钱数
            //若修改前大于修改后则余额减去修改前与修改后的差
            if(salary1.getsMoney().compareTo(salary.getsMoney())==1&&salary.getsStatus()!=0){
                sysUser.setUserId(salary.getsName());
                sysUser.setBalance(sysUser1.getBalance().subtract(salary1.getsMoney().subtract(salary.getsMoney())));
                sysUserService.updateUser(sysUser);
                    //若修改前小于修改后则余额加上修改前与修改后的差
            }else if(salary1.getsMoney().compareTo(salary.getsMoney())==-1){
//                if(salary.getsMoney()>salary1.getsBalance()&&salary.getsStatus()==0){
//                    Salary salary2 =new Salary();
//                    salary2.setsId(salary.getsId());
//                    salary2.setsStatus(2);
//                    return toAjax(salaryService.updateSalary(salary2));
//                }
                sysUser.setUserId(salary.getsName());
                sysUser.setBalance(sysUser1.getBalance().add(salary.getsMoney().subtract(salary1.getsMoney())));
                sysUserService.updateUser(sysUser);
            }
        return toAjax(salaryService.updateSalary(salary));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @RequiresPermissions("system:salary:remove")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
//    @PostMapping( "/remove")
//    @ResponseBody
//    public AjaxResult remove(String ids)
//    {
//        Long id=Long.parseLong(ids);
//        Salary salary=salaryService.selectSalaryById(id);
//        SysUser sysUser=new SysUser();
//        //通过id查询员工此时的余额
//        SysUser sysuser= sysUserService.selectUser(salary.getsName());
//        //判断工资是否下发未下发删除时减去余额
//        if(salary.getsStatus()==1){
//            sysUser.setUserId(salary.getsName());
//            sysUser.setBalance(sysuser.getBalance()-salary.getsMoney());
//            sysUserService.updateUser(sysUser);
//        }else if(salary.getsStatus()==2){
//
//                sysUser.setUserId(salary.getsName());
//                sysUser.setBalance(salary.getsMoney()-sysuser.getBalance()-salary.getsBalance());
//                sysUserService.updateUser(sysUser);
//
//        }
//        return toAjax(salaryService.deleteSalaryByIds(ids));
//    }
}
